package com.jd.nettydemo.server;

import com.jd.nettydemo.domain.RequestMessage;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by wymaoxiaoliang on 2017/3/1.
 */
public class KryoCodecServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(final ChannelHandlerContext ctx) {
        RequestMessage msg = new RequestMessage();
        msg.setAppId("ssfTestAppID@server");
        msg.setServiceName("hello@server");
        msg.setRequestTime(System.currentTimeMillis());
        ChannelFuture f = ctx.writeAndFlush(msg);
        System.out.println("server write msg:" + msg);
        f.addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        // 当出现异常就关闭连接
        cause.printStackTrace();
        ctx.close();
    }
}
